package com.uestc.service.impl;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.uestc.mapper.NewsMapper;
import com.uestc.mapper.PartMapper;
import com.uestc.mapper.TrainingListMapper;
import com.uestc.pojo.News;
import com.uestc.pojo.NewsExample;
import com.uestc.pojo.NewsExample.Criteria;
import com.uestc.pojo.Part;
import com.uestc.pojo.PartExample;
import com.uestc.pojo.TrainingList;
import com.uestc.pojo.TrainingListExample;
import com.uestc.service.IPageService;

@Service
public class PageServiceImpl implements IPageService {
	@Autowired
	private NewsMapper newsMapper;
	@Autowired
	private PartMapper partMapper;
	@Autowired
	private TrainingListMapper trainingListMapper;

	@Override
	public Map<String, List<News>> selectNewsForIndex() {
		Map<String, List<News>> map = new LinkedHashMap<>();
		map.put("1", selectNewsByCategoryIdAndRows(Long.valueOf(1), 8));
		map.put("2", selectNewsByCategoryIdAndRows(Long.valueOf(2), 8));
		map.put("3", selectNewsByCategoryIdAndRows(Long.valueOf(3), 8));
		map.put("4", selectNewsByCategoryIdAndRows(Long.valueOf(4), 8));
		map.put("6", selectNewsByCategoryIdAndRows(Long.valueOf(5), 8));
		return map;
	}
	@Override
	public List<News> selectfocusNewsForIndex() {
		return selectNewsByCategoryIdAndRows(Long.valueOf(0), 3);
	}

	private List<News> selectNewsByCategoryIdAndRows(Long categoryId, Integer rows) {
		PageHelper.startPage(0, rows);
		NewsExample example = new NewsExample();
		Criteria c = example.createCriteria();
		c.andCategoryIdEqualTo(categoryId);
		example.setOrderByClause("create_time desc");
		List<News> list = newsMapper.selectByExample(example);
		new PageInfo<>(list);
		return list;
	}
	@Override
	public News selectNext(Long categoryId,String id) {
		News news = newsMapper.selectByPrimaryKey(id);
		NewsExample example = new NewsExample();
		Criteria c1 = example.createCriteria();
		c1.andCreateTimeGreaterThan(news.getCreateTime());
		c1.andCategoryIdEqualTo(categoryId);
		example.setOrderByClause("create_time ASC LIMIT 1");
		List<News> newsList = newsMapper.selectByExample(example);
		if(newsList.isEmpty()) {
			return news;
		}else {
			return newsList.get(0);
		}
	}
	@Override
	public News selectPrevious(Long categoryId,String id) {
		News news = newsMapper.selectByPrimaryKey(id);
		NewsExample example = new NewsExample();
		Criteria c1 = example.createCriteria();
		c1.andCreateTimeLessThan(news.getCreateTime());
		c1.andCategoryIdEqualTo(categoryId);
		example.setOrderByClause("create_time DESC LIMIT 1");
		List<News> newsList = newsMapper.selectByExample(example);
		if(newsList.isEmpty()) {
			return news;
		}else {
			return newsList.get(0);
		}
	}
	@Override
	public Part selectPartyNext(Long id) {
		Part part = partMapper.selectByPrimaryKey(id);
		PartExample example=new PartExample();
		com.uestc.pojo.PartExample.Criteria c1 = example.createCriteria();
		c1.andCreateTimeGreaterThan(part.getCreateTime());
		example.setOrderByClause("create_time ASC LIMIT 1");
		List<Part> partList = partMapper.selectByExample(example);
		if(partList.isEmpty()) {
			return part;
		}else {
			return partList.get(0);
		}
	}
	@Override
	public Part selectPartyPrevious(Long id) {
		Part part = partMapper.selectByPrimaryKey(id);
		PartExample example=new PartExample();
		com.uestc.pojo.PartExample.Criteria c1 = example.createCriteria();
		c1.andCreateTimeLessThan(part.getCreateTime());
		example.setOrderByClause("create_time DESC LIMIT 1");
		List<Part> partList = partMapper.selectByExample(example);
		if(partList.isEmpty()) {
			return part;
		}else {
			return partList.get(0);
		}
	}
	
	@Override
	public TrainingList selectTrainingNext(Integer parentId,Integer  id) {
		TrainingList part = trainingListMapper.selectByPrimaryKey(id);
		TrainingListExample example=new TrainingListExample();
		com.uestc.pojo.TrainingListExample.Criteria c1 = example.createCriteria();
		c1.andCreateTimeGreaterThan(part.getCreateTime());
		c1.andParentIdEqualTo(parentId);
		example.setOrderByClause("create_time ASC LIMIT 1");
		List<TrainingList> partList = trainingListMapper.selectByExample(example);
		if(partList.isEmpty()) {
			return part;
		}else {
			return partList.get(0);
		}
	}
	@Override
	public TrainingList selectTrainingPrevious(Integer parentId,Integer id) {
		TrainingList part = trainingListMapper.selectByPrimaryKey(id);
		TrainingListExample example=new TrainingListExample();
		com.uestc.pojo.TrainingListExample.Criteria c1 = example.createCriteria();
		c1.andCreateTimeLessThan(part.getCreateTime());
		c1.andParentIdEqualTo(parentId);
		example.setOrderByClause("create_time DESC LIMIT 1");
		List<TrainingList> partList = trainingListMapper.selectByExample(example);
		if(partList.isEmpty()) {
			return part;
		}else {
			return partList.get(0);
		}
	}

}
